package com.best.member.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.best.core.entity.CommonResult;
import com.best.core.exception.CommonException;
import com.best.core.exception.CommonExceptionCode;
import com.best.core.exception.CommonExceptionEnum;

import lombok.extern.slf4j.Slf4j;


/**
 * @ClassName: BaseExceptionHandler
 * @Package: com.best.member.advice
 * @Description: 统一异常处理类
 * @Datetime: 2020/6/5 17:54
 * @Author: nike
 */
@ControllerAdvice
@Slf4j
public class BaseExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult error(Exception e) {
        // 记录错误日志
        log.error("[ bset-shop-service-member ] throw exception is : {}", e);
        if (e instanceof CommonException) {
            CommonExceptionCode commonExceptionCode =
                    ((CommonException) e).getCommonExceptionCode();
            return CommonResult.buildFail(commonExceptionCode.getFlag(),
                    commonExceptionCode.getCode(), e.getMessage());
        }
        return CommonResult.buildFail(CommonExceptionEnum.SYSTEM_IS_ERROR.getMsg());
    }
}
